<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Info\Mapper;

use App\Info\Model\InfoClass;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 文章分类Mapper类
 */
class InfoClassMapper extends AbstractMapper
{
    /**
     * @var InfoClass
     */
    public $model;

    public function assignModel()
    {
        $this->model = InfoClass::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // ID
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 分类名称
        if (isset($params['name']) && filled($params['name'])) {
            $query->where('name', 'like', '%'.$params['name'].'%');
        }

        // 父类id
        if (isset($params['parent_id']) && filled($params['parent_id'])) {
            $query->where('parent_id', '=', $params['parent_id']);
        }

        // 排序
        if (isset($params['display_order']) && filled($params['display_order'])) {
            $query->where('display_order', '=', $params['display_order']);
        }

        // 创建者
        if (isset($params['created_by']) && filled($params['created_by'])) {
            $query->where('created_by', '=', $params['created_by']);
        }

        // 更新者
        if (isset($params['updated_by']) && filled($params['updated_by'])) {
            $query->where('updated_by', '=', $params['updated_by']);
        }

        // 状态
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 创建时间
        if (isset($params['create_time']) && filled($params['create_time'])) {
            $query->where('create_time', '=', $params['create_time']);
        }

        // 更新时间
        if (isset($params['update_time']) && filled($params['update_time'])) {
            $query->where('update_time', '=', $params['update_time']);
        }

        return $query;
    }

    public function getConfigInfo($params)
    {
        $settingConfig = \App\Setting\Model\SettingConfig::query()
            ->whereIn('key', [
                'company_addr',
                'company_addr_img',
                'company_email',
                'company_en_name',
                'company_name',
                'company_official_img',
                'company_red_img',
                'company_tel',
                'company_link',
                'code_body'
            ])
            ->pluck('value', 'key');
          
            $settingConfig['code_body']=empty($settingConfig['code_body'])?[]:json_decode($settingConfig['code_body'],true);
        return $settingConfig;
    }

    public function submitConfig($params)
    {
        \App\Setting\Model\SettingConfig::query()
            ->where('key', 'company_addr')
            ->update(['value' => $params['company_addr']]);
        \App\Setting\Model\SettingConfig::query()
            ->where('key', 'company_addr_img')
            ->update(['value' => $params['company_addr_img']]);
        \App\Setting\Model\SettingConfig::query()
            ->where('key', 'company_email')
            ->update(['value' => $params['company_email']]);
        \App\Setting\Model\SettingConfig::query()
            ->where('key', 'company_en_name')
            ->update(['value' => $params['company_en_name']]);
        \App\Setting\Model\SettingConfig::query()
            ->where('key', 'company_name')
            ->update(['value' => $params['company_name']]);

        \App\Setting\Model\SettingConfig::query()
            ->where('key', 'company_tel')
            ->update(['value' => $params['company_tel']]);
            \App\Setting\Model\SettingConfig::query()
            ->where('key', 'code_body')
            ->update(['value' => empty($params['code_body'])?json_encode([]):json_encode($params['code_body'])]);
       
        return [];
    }

    public function getConfigInfo2($params)
    {
        $settingConfig = \App\Setting\Model\SettingConfig::query()
            ->whereIn('key', [
                'culture_title',
                'culture_title_img',
                'culture_desc',
                'culture_icon',
                'culture_right_img',
            ])
            ->pluck('value', 'key');

        $settingConfig['code_body']=empty($settingConfig['code_body'])?[]:json_decode($settingConfig['code_body'],true);
        return $settingConfig;
    }

    public function submitConfig2($params)
    {
        \App\Setting\Model\SettingConfig::query()
            ->where('key', 'culture_title')
            ->update(['value' => $params['culture_title']]);
        \App\Setting\Model\SettingConfig::query()
            ->where('key', 'culture_title_img')
            ->update(['value' => $params['culture_title_img']]);
        \App\Setting\Model\SettingConfig::query()
            ->where('key', 'culture_desc')
            ->update(['value' => $params['culture_desc']]);
        \App\Setting\Model\SettingConfig::query()
            ->where('key', 'culture_icon')
            ->update(['value' => $params['culture_icon']]);
        \App\Setting\Model\SettingConfig::query()
            ->where('key', 'culture_right_img')
            ->update(['value' => $params['culture_right_img']]);

        return [];
    }

}